package com.ky.tms.controller;


import com.ky.tms.entity.TmsUser;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsUserService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import com.ky.tms.util.RequestContextHolderUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-03-21
 */
@RestController
@RequestMapping("/tms/tms-user")
public class TmsUserController {

    @Autowired
    private ITmsUserService tmsUserService;

    @ApiOperation("用户数据分页")
    @GetMapping("page")
    public CommonResult page(TmsUser tmsUser, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsUserService.page(userName, tmsUser, pageSize, current));
    }

    @ApiOperation("创建用户")
    @PostMapping
    public CommonResult add(@RequestBody TmsUser tmsUser, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsUserService.add(userName,tmsUser);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改用户")
    @PutMapping
    public CommonResult update(@RequestBody TmsUser tmsUser, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsUserService.update(userName,tmsUser);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改密码")
    @PostMapping("updatePassword")
    public CommonResult updatePassWord(@RequestBody TmsUser tmsUser) throws Exception {
        tmsUserService.updatePassword(tmsUser);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("重置密码")
    @PostMapping("resetPassword")
    public CommonResult resetPassword(@RequestBody TmsUser tmsUser) throws Exception {
        tmsUserService.resetPassword(tmsUser);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("删除用户")
    @DeleteMapping
    public CommonResult delete(@RequestBody List<Long> ids, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        tmsUserService.delete(userName, ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("搜索下拉数据")
    @GetMapping("select-list")
    public CommonResult selectList(HttpServletRequest request) {
        return CommonResult.buildSuccess(tmsUserService.selectList());
    }

    @ApiOperation("获取当前用户")
    @GetMapping("current")
    public Map<String, Object> current(){
        Map<String, Object> result = new HashMap<>();
        String role = RequestContextHolderUtil.getRole();
        String userName = RequestContextHolderUtil.getUserName();
        result.put("name", userName);
        result.put("role",role);
        result.put("avatar","https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png");
        return result;

    }

}
